package main.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import main.model.DataModel;
import main.model.Day;


/**
 *
 * Draws a bar graph that shows the number of days spend in a
 * particular price range over a period of time
 *
 * @author Seon Kang
 *
 */
public class BarChart extends Visualizer {

    private static final int NUMBEROFBARS = 8;
    private static final int BARWIDTH = 5;
    private static final int NUMBEROFMARKS = 10;
    private final int myBaseValue = 0;
    private int myHorizontalIncrement = 0;
    private int myVerticalIncrement = 0;
    private int myBarChartArrayMax;
    private float myMax = 0;
    private float myMin = 0;
    private Graphics myPen;
    private int[] myBarChartArray;

    /**
     * BarChart constructor.
     *
     * @param dataModel data model to initialize with
     * @param size size of visualization
     */
    public BarChart (DataModel dataModel, Dimension size) {
        super(dataModel, size);
        setMyXAxis("Price Range");
        setMyYAxis("Days Spent in Range");
    }

    protected int findMaxElement (int[] a) {
        int max = a[0];
        for (int i : a) {
            if (i > max) {
                max = i;
            }
        }
        return max;
    }

    @Override
    public void paint (Graphics pen) {
        myPen = pen;
        DataModel data = getData();
        myMin = data.getMinRow().getLow();
        myMax = data.getMaxRow().getHigh();
        myHorizontalIncrement = getMyWidth() / NUMBEROFBARS;
        myBarChartArray = new int[NUMBEROFBARS];
        fillBarChartArray();
        myBarChartArrayMax = findMaxElement(myBarChartArray);
        if (myBarChartArrayMax == 0) {
            myBarChartArrayMax = 1;
        }
        myVerticalIncrement = getMyHeight() / myBarChartArrayMax;
        labelYAxis(NUMBEROFMARKS, myBarChartArrayMax, myBaseValue, myPen);
        drawBars();
        super.paint(pen);
    }

    protected void drawBars() {
        for (int i = 0; i < NUMBEROFBARS; i++) {
            myPen.setColor(Color.RED);
            int topOfBar =
                    getMyHeight() - (myBarChartArray[i] * myVerticalIncrement) - getBottomMargin();
            myPen.drawRect(getLeftMargin() + myHorizontalIncrement * i, topOfBar, BARWIDTH,
                         myBarChartArray[i] * myVerticalIncrement);
        }

    }

    protected void fillBarChartArray() {
        float priceIncrement = (myMax - myMin) / NUMBEROFBARS;
        float rightPriceIndex = myMin + priceIncrement;
        float leftPriceIndex = myMin;
        int leftIndex = 0;
        for (int i = 0; i < NUMBEROFBARS; i++) {
            // Also labels the x-Axis in the process due to convenience
            myPen.setColor(Color.GRAY);
            myPen.drawString((int) leftPriceIndex + " - " + (int) rightPriceIndex, 2 + leftIndex +
                    getLeftMargin(), getMyHeight() - getBottomMargin() / 2);
            for (Day d : getData()) {
                int average = (int) (d.getHigh() + d.getLow()) / 2;
                if (average < rightPriceIndex && average >= leftPriceIndex) {
                    myBarChartArray[i] = myBarChartArray[i] + 1;
                }
            }
            leftIndex += myHorizontalIncrement;
            rightPriceIndex += priceIncrement;
            leftPriceIndex += priceIncrement;
        }
    }
}
